package com.driving.single.frontend.user.controller;

import com.driving.single.common.entity.DrivingResult;
import com.driving.single.common.utils.RegexUtils;
import com.driving.single.frontend.user.service.FrontendUserService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "/frontend/user", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class FrontendUserController {
    @Resource
    private FrontendUserService frontendUserService;

    /**
     * 用户登录(帐号密码)接口
     *
     * @param account  帐号
     * @param password 密码
     */
    @PostMapping("/login")
    public DrivingResult userLogin(@RequestParam("account") String account, @RequestParam("password") String password) {
        try {
            return this.frontendUserService.userLogin(account, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DrivingResult.error("error");
    }

    /**
     * 用户登录 手机验证码发送 接口
     *
     * @param phone 用户注册手机号码
     */
    @RequestMapping("/sendLoginSms/{phone}")
    public DrivingResult sendLoginSms(@PathVariable String phone) {
        try {
            // 1.手机号码校验
            boolean isPhone = RegexUtils.isPhone(phone);
            if (!isPhone) {
                return DrivingResult.error("请输入正确的手机号码");
            }
            return this.frontendUserService.sendLoginSms(phone);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DrivingResult.error("error");
    }

    /**
     * 用户登录(手机验证码)接口
     *
     * @param phone 用户注册手机号码
     * @param code  验证码
     */
    @RequestMapping("/loginWithPhoneCode")
    public DrivingResult userLoginWithPhoneCode(String phone, String code) {
        try {
            // 1.手机号码校验
            boolean isPhone = RegexUtils.isPhone(phone);
            if (!isPhone) {
                return DrivingResult.error("请输入正确的手机号码");
            }
            return this.frontendUserService.userLoginWithPhoneCode(phone, code);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DrivingResult.error("error");
    }

    /**
     * 用户注册 手机验证码发送 接口
     *
     * @param phone 用户注册手机号码
     */
    @RequestMapping("/sendRegisterSms/{phone}")
    public DrivingResult sendRegisterSms(@PathVariable String phone) {
        try {
            // 1.手机号码校验
            boolean isPhone = RegexUtils.isPhone(phone);
            if (!isPhone) {
                return DrivingResult.error("请输入正确的手机号码");
            }
            return this.frontendUserService.sendRegisterSms(phone);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DrivingResult.error("error");
    }

    /**
     * 用户注册(手机验证码) 接口
     *
     * @param phone 用户注册手机号码
     * @param code  验证码
     */
    @RequestMapping("/registerWithPhoneCode")
    public DrivingResult userRegisterWithCode(String phone, String code) {
        try {
            // 1.手机号码校验
            boolean isPhone = RegexUtils.isPhone(phone);
            if (!isPhone) {
                return DrivingResult.error("请输入正确的手机号码");
            }
            return this.frontendUserService.userRegisterWithCode(phone, code);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DrivingResult.error("error");
    }

    /**
     * 检查手机号是否重复 接口
     *
     * @param phone 手机号码
     */
    @RequestMapping("/checkPhone/{phone}")
    public DrivingResult checkPhone(@PathVariable String phone) {
        try {
            // 1.手机号码校验
            boolean isPhone = RegexUtils.isPhone(phone);
            if (!isPhone) {
                return DrivingResult.error("请输入正确的手机号码");
            }
            return this.frontendUserService.checkPhone(phone);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DrivingResult.error("error");
    }

    /**
     * 检查用户名是否重复 接口
     *
     * @param username 用户名
     */
    @RequestMapping("/checkUsername")
    public DrivingResult checkUsername(String username) {
        try {
            return this.frontendUserService.checkUsername(username);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DrivingResult.error("error");
    }

    /**
     * 根据 用户ID 获取用户信息 接口
     *
     * @param uid 用户ID
     */
    @RequestMapping("/sso/selectTbUserByUid")
    public DrivingResult selectTbUserById(Long uid) {
        try {
            return this.frontendUserService.selectTbUserById(uid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DrivingResult.error("error");
    }


    /**
     * 用户评论 接口
     *
     * @param uid        用户ID
     * @param targetType 目标(驾校/教练/陪练)类型,1:驾校，2:教练，3:陪练
     * @param targetId   目标(驾校/教练/陪练)ID
    @RequestMapping("/sso/userComment")
    public DrivingResult userComment(Long uid, String targetType, Long targetId) {
        try {
            return this.frontendUserService.userComment(uid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DrivingResult.error("error");
    }*/


}
